package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.entity.Banner;
import cn.tedu.coolshark.mapper.BannerMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.util.List;

@RestController
public class BannerController {

    @Value("${dirPath}")
    private String dirPath;

    @Autowired(required = false)
    BannerMapper mapper;

    @RequestMapping("/banner/select")
    public List<Banner> select(){

        return mapper.select();

    }

    @RequestMapping("/banner/delete")
    public void deleteById(int id){

        //先查询到轮播图的url
        String url = mapper.selectUrlById(id);
        //aaa.jpg    D:/files/aaa.jpg
        //得到文件的完整磁盘路径
        String filePath = dirPath+"/"+url;
        //创建文件对象并删除
        new File(filePath).delete();
        System.out.println("id = " + id);
        mapper.deleteById(id);

    }

    @RequestMapping("/banner/insert")
    //声明Banner对象接收
    public void insert(@RequestBody Banner banner){

        System.out.println("banner = " + banner);
        mapper.insert(banner);

    }

}
